import { jsonToVec3 } from "../core/common.js";
// 旋转
export const Rotate = {
  frameAction: null,
  start(options = {
    point: [[114.00861755852046, 22.6296604931908, 90]],
    degrees: 0.08
  }, cb) {
    let scene = GlobalViewer.scene;
    let camera = scene.mainCamera;
    let cameraController = camera.cameraController();

    let point = jsonToVec3(options.point);
    let degrees = options.degrees; //每次旋转的度数
    let nowdegrees = 0; //累计的度数
    point = Array.isArray(point) ? point[0] : point
    let destination = point.toCartesian3();
    let newpitch = cameraController.pitch;
    let newroll = cameraController.roll;
    let initialHeading = cameraController.heading; //相机当前heading
    let distance = cameraController.positionWC.distance(point); //当前视角到中心点距离
    this.frameAction = new SSmap.FrameAction();
    this.frameAction.onTriggered(function (delta) {
      nowdegrees += degrees;
      let radians = (Math.PI / 180) * nowdegrees;
      let nowheading = radians + initialHeading;
      cameraController.setView(
        destination,
        nowheading,
        newpitch,
        newroll
      );
      cameraController.moveBackward(distance); //空间距离
    });
    GlobalViewer.scene.rootEntity.addComponent(this.frameAction);
    cb && cb();
  },
  stop(cb) {
    if (this.frameAction) {
      this.frameAction.delete();
      this.frameAction = null;
    }
    cb && cb();
  },
  isOn(cb) {
    cb && cb(this.frameAction ? true : false)
  }
};